<?php
	class Migration_1_0_11 extends GigaMigration
	{
		function migrate($migrateShell)
		{
			$this->fixEavIntFloat($migrateShell);
			$this->fixEavDateTimeToDate($migrateShell);
		}
		
		protected function fixEavIntFloat($migrateShell)
		{
			App::import("Model","CustomField");
			App::import("Model","UserField");
			App::import("Model","Eav.EavValue");
			$CustomFieldModel = cri("CustomField");
			$UserFieldModel = cri("UserField");
			$EavIntModel = cri("EavValuesInt");
			$EavIntModel->bindModel(array("belongsTo"=>array("EavRecord")),false);
			$EavFloatModel = cri("EavValuesFloat");
			$intFieldsOfListing =  $CustomFieldModel->find("list",array("fields"=>array("id",'name'),'conditions'=>array("type"=>array("number"))));
			$eavInts = $EavIntModel->find("all",array("conditions"=>array("EavValuesInt.field"=>$intFieldsOfListing,"EavRecord.entity"=>array("Listing","Review"))));
			$intFieldsOfUser = $UserFieldModel->find("list",array("fields"=>array("id",'name'),'conditions'=>array("type"=>array("number"))));
			$userEavInts = $EavIntModel->find("all",array("conditions"=>array("EavValuesInt.field"=>$intFieldsOfUser,"EavRecord.entity"=>array("User"))));
			$eavInts = am($eavInts,$userEavInts);
			$migrateShell->out("Found ".count($eavInts)." records to be moved to float model");
			foreach($eavInts as $eavInt)
			{
				$record = 	$eavInt[$EavIntModel->alias];
				$recordId = $record['id'];
				unset($record['id']);
				$floatData = array();
				$floatData[$EavFloatModel->alias] = $record;
				$EavFloatModel->create();
				$EavFloatModel->save($floatData);
				$EavIntModel->del($recordId);	
			}
		}
		
		protected function fixEavDateTimeToDate($migrateShell)
		{
			App::import("Model","CustomField");
			App::import("Model","UserField");
			App::import("Model","Eav.EavValue");
			$CustomFieldModel = cri("CustomField");
			$UserFieldModel = cri("UserField");
			$EavDatetimeModel = cri("EavValuesDatetime");
			$EavDatetimeModel->bindModel(array("belongsTo"=>array("EavRecord")),false);
			$EavDateModel = cri("EavValuesDate");
			$dateTimeFieldsOfListing =  $CustomFieldModel->find("list",array("fields"=>array("id",'name'),'conditions'=>array("type"=>array("datePicker"))));
			$eavdts = $EavDatetimeModel->find("all",array("conditions"=>array("EavValuesDatetime.field"=>$dateTimeFieldsOfListing,"EavRecord.entity"=>array("Listing","Review"))));
			$dateTimeFieldsOfUser = $UserFieldModel->find("list",array("fields"=>array("id",'name'),'conditions'=>array("type"=>array("datePicker"))));
			$userEavdts = $EavDatetimeModel->find("all",array("conditions"=>array("EavValuesDatetime.field"=>$dateTimeFieldsOfUser,"EavRecord.entity"=>array("User"))));
			$eavdts = am($eavdts,$userEavdts);
			$migrateShell->out("Found ".count($eavdts)." records to be moved to date model");
			foreach($eavdts as $eavdt)
			{
				$record = 	$eavdt[$EavDatetimeModel->alias];
				$recordId = $record['id'];
				unset($record['id']);
				$dateData = array();
				$dateData[$EavDateModel->alias] = $record;
				$EavDateModel->create();
				$EavDateModel->save($dateData);
				$EavDatetimeModel->del($recordId);	
			}
		}
	}
?>